package com.yida.gwj.finance.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.Logical;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceAccountMain;
import com.yida.gwj.api.domain.dto.GwjFinanceAccountMainDTO;
import com.yida.gwj.api.domain.request.GwjFinanceAccountMainRequest;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountMainRequest;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.enums.GwjCheckAccountStatus;
import com.yida.gwj.common.enums.GwjCheckAccountType;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.finance.service.IGwjFinanceAccountMainService;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author 缴存业务管理模块
 * @date 2023-01-10
 */
@RestController
@RequestMapping("/accountMain")
@Api(tags = "财务模块（总账流水）")
public class GwjFinanceAccountMainController extends BaseController {

    @Resource
    private IGwjFinanceAccountMainService gwjFinanceAccountMainService;

    @ApiOperation(value = "查询总账流水列表", response = GwjFinanceAccountMain.class)
    @RequiresPermissions(value = {"gwj:finance:accountMain:list", "gwj:finance:bank:list"}, logical = Logical.OR)
    @GetMapping("/list")
    public TableDataInfo list(GwjFinanceAccountMainRequest request) {
        GwjFinanceAccountMain gwjFinanceAccountMain = GwjConvertUtil.convert(request, GwjFinanceAccountMain.class);
        startPage();
        List<GwjFinanceAccountMain> list = gwjFinanceAccountMainService.selectGwjFinanceAccountMainList(gwjFinanceAccountMain);
        list.forEach(item -> {
            item.setBizFlagIdStr(GwjBusinessFlagType.getInfoByCode(item.getBizFlagId()));
            item.setCheckTypeStr(GwjCheckAccountType.getInfoByCode(item.getCheckType()));
            item.setCheckStatusStr(GwjCheckAccountStatus.getInfoByCode(item.getCheckStatus()));
        });
        return GwjConvertUtil.convertTable(getDataTable(list), GwjFinanceAccountMainDTO.class);
    }

    @ApiOperation("导出总账流水列表")
    @RequiresPermissions("gwj:finance:accountMain:export")
    @Log(title = "总账流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwjFinanceAccountMain gwjFinanceAccountMain) {
        List<GwjFinanceAccountMain> list = gwjFinanceAccountMainService.selectGwjFinanceAccountMainList(gwjFinanceAccountMain);
        ExcelUtil<GwjFinanceAccountMain> util = new ExcelUtil<GwjFinanceAccountMain>(GwjFinanceAccountMain.class);
        util.exportExcel(response, list, "总账流水数据");
    }

    @ApiOperation("获取总账流水详细信息")
    @RequiresPermissions("gwj:finance:accountMain:query")
    @GetMapping(value = "/{accountMainId}")
    public AjaxResult getInfo(@PathVariable("accountMainId") Long accountMainId) {
        return AjaxResult.success(gwjFinanceAccountMainService.selectGwjFinanceAccountMainByAccountMainId(accountMainId));
    }

    @ApiOperation("新增总账流水")
    @RequiresPermissions("gwj:finance:accountMain:add")
    @Log(title = "总账流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return toAjax(gwjFinanceAccountMainService.insertGwjFinanceAccountMain(gwjFinanceAccountMain));
    }

    @ApiOperation("修改总账流水")
    @RequiresPermissions("gwj:finance:accountMain:edit")
    @Log(title = "总账流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return toAjax(gwjFinanceAccountMainService.updateGwjFinanceAccountMain(gwjFinanceAccountMain));
    }

    @ApiOperation("删除总账流水")
    @RequiresPermissions("gwj:finance:accountMain:remove")
    @Log(title = "总账流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountMainIds}")
    public AjaxResult remove(@PathVariable Long[] accountMainIds) {
        return toAjax(gwjFinanceAccountMainService.deleteGwjFinanceAccountMainByAccountMainIds(accountMainIds));
    }

    /**
     * 根据业务ID删除总账流水
     */
    @InnerAuth
    @DeleteMapping("/inner/{bizId}")
    public R<Integer> deleteGwjFinanceAccountMainByBizId(@PathVariable("bizId") Long bizId) {
        return R.ok(gwjFinanceAccountMainService.deleteGwjFinanceAccountMainByBizId(bizId));
    }

    /**
     * 新增总账流水
     */
    @InnerAuth
    @PostMapping("/inner")
    public R<Integer> insertGwjFinanceAccountMain(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return R.ok(gwjFinanceAccountMainService.insertGwjFinanceAccountMain(gwjFinanceAccountMain));
    }

    /**
     * 修改总账流水
     */
    @InnerAuth
    @PutMapping("/inner")
    public R<Integer> updateGwjFinanceAccountMain(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return R.ok(gwjFinanceAccountMainService.updateGwjFinanceAccountMain(gwjFinanceAccountMain));
    }

    /**
     * 根据业务ID查询类帐信息
     */
    @InnerAuth
    @GetMapping("/inner/{bizId}")
    public R<GwjFinanceAccountMain> selectGwjFinanceAccountMainByBizId(@PathVariable("bizId") Long bizId) {
        return R.ok(gwjFinanceAccountMainService.selectGwjFinanceAccountMainByBizId(bizId));
    }

    /**
     * 查询总账流水列表
     */
    @InnerAuth
    @PostMapping("/inner/list")
    public R<List<GwjFinanceAccountMain>> selectGwjFinanceAccountMainList(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return R.ok(gwjFinanceAccountMainService.selectGwjFinanceAccountMainList(gwjFinanceAccountMain));
    }

    /**
     * 查询总账流水列表
     */
    @InnerAuth
    @PostMapping("/inner/page")
    public R<TableDataInfo> selectGwjFinanceAccountMainPage(@RequestBody RemoteGwjFinanceAccountMainRequest gwjFinanceAccountMainRequest) {
        PageUtils.startPage(gwjFinanceAccountMainRequest.getPageNum(), gwjFinanceAccountMainRequest.getPageSize());
        return R.ok(getDataTable(gwjFinanceAccountMainService.selectGwjFinanceAccountMainList(BeanUtil.copyProperties(gwjFinanceAccountMainRequest, GwjFinanceAccountMain.class))));
    }

    /**
     * 查询总账流水
     */
    @InnerAuth
    @PostMapping("/getGwjFinanceAccountMain")
    public R<GwjFinanceAccountMain> getGwjFinanceAccountMain(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return R.ok(gwjFinanceAccountMainService.getGwjFinanceAccountMain(gwjFinanceAccountMain));
    }

    /**
     * 删除总账流水
     */
    @InnerAuth
    @PostMapping("/deleteGwjFinanceAccountMain")
    public R<Integer> deleteGwjFinanceAccountMain(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return R.ok(gwjFinanceAccountMainService.deleteGwjFinanceAccountMain(gwjFinanceAccountMain));
    }

    /**
     * 修改总账流水
     */
    @InnerAuth
    @PutMapping("/inner/mainInfo")
    public R<Integer> updateGwjFinanceAccountMainInfo(@RequestBody GwjFinanceAccountMain gwjFinanceAccountMain) {
        return R.ok(gwjFinanceAccountMainService.updateGwjFinanceAccountMainInfo(gwjFinanceAccountMain));
    }

}
